package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.lang.annotation.Documented;
import java.util.ArrayList;
import java.util.List;

/**
 * 新增菜品
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 菜品添加
     * @param dishDto
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody DishDto dishDto){
        log.info("接收菜品添加数据:{}",dishDto.toString());

        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        // dishService中创建该方法,调用两张表进行存储
        dishService.saveWithFlavor(dishDto);

        return R.success("菜品添加成功!");
    }

    /**
     * 分页查询(菜品查询)
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){
        log.info("查询页码:{},查询条数:{}",page,pageSize);

        // 构造分页查询器
        Page<Dish> dishPage = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();

        // 条件构造器
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null,Dish::getName,name);

        // 排序
        queryWrapper.orderByAsc(Dish::getCreateTime);

        // 执行分页查询
        dishService.page(dishPage, queryWrapper);

        // 数据拷贝(将dishPage中的属性的值拷贝到dishDtoPage中相同属性下,records封装数据,由于两个records的类型不一样,所以不能直接拷贝)
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");     // 这样dishDtoPage中就只差查询的数据集合了
        // 获取dishPage中的数据集合
        List<Dish> records = dishPage.getRecords();
        // 创建一个集合,用于接收DishDto类型的records数据
        List<DishDto> records1 = new ArrayList<>();
        // 遍历dish类型的records数据,并分别添加到dishDto类型的records中
        for (Dish record : records) {
            // 创建dishDto对象
            DishDto dishDto = new DishDto();
            // 将单条dish类型的数据拷贝到dishDto对象中
            BeanUtils.copyProperties(record,dishDto);

            // 获取分类id
            Long categoryId = record.getCategoryId();
            // 根据分类id查询该条分类信息
            Category category = categoryService.getById(categoryId);
            // 获取该分类的名称
            String name1 = category.getName();
            // 判断分类名称是否为空
            if (name1 != null){
                // 设置dishDto类中的分类名称
                dishDto.setCategoryName(name1);
            }
            records1.add(dishDto);
        }
        // 将DishDto类型的数据信息集合records1封装到DishDtoPage类中
        dishDtoPage.setRecords(records1);

        return R.success(dishDtoPage);
    }

    /**
     * 菜品修改返回菜品信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> get(@PathVariable Long id){
        log.info("接收查询id:{}",id);

        // 调用service中的方法,返回查询结果封装类
        DishDto dishDto = dishService.getByIdWithFlavor(id);

        return R.success(dishDto);
    }

    /**
     * 菜品修改
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody DishDto dishDto){
        log.info("接收菜品添加数据:{}",dishDto.toString());

        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        // dishService中创建该方法,调用两张表进行存储
        dishService.updateWithFlavor(dishDto);

        return R.success("菜品添加成功!");
    }

    /**
     * 菜品起售或停售
     * @param sta   起售或停售标识
     * @param ids   修改菜品id集合
     * @return
     */
    @PostMapping("/status/{sta}")
    public R<String> updateStatus(HttpServletRequest request,@PathVariable int sta,@RequestParam List<Long> ids){
        log.info("操作:{},ids:{}",sta,ids);

        Long employee = (Long) request.getSession().getAttribute("employee");
        BaseContext.setCurrentId(employee);

        dishService.updateStatus(sta,ids);

        return R.success("修改成功!");
    }

    /**
     * 菜品删除
     * @param ids   接收准备删除的菜品id
     * @return
     */
    @DeleteMapping
    public R<String> deleteDish(@RequestParam List<Long> ids){
        log.info("接收到删除ids:{}",ids);

        // 要删除两张表中的数据: dish  dish_flavor
        dishService.deleteDish(ids);

        return R.success("删除成功!");
    }

    /**
     * 查询分类下的菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> getDish(Long categoryId){
        log.info("菜品分类id:{}",categoryId);

        List<DishDto> dish = dishService.getDish(categoryId);

        return R.success(dish);
    }

}
